package org.wjw.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");
        // 获取session
        HttpSession session = req.getSession();
        // 本身就为空，直接跳转到登录页
        if (session == null) {
            resp.sendRedirect("blog_login.html");
            return;
        }
        // 销毁session
        session.invalidate();
        // 跳转到登录页
        resp.sendRedirect("blog_login.html");
    }
}
